package com.screen.screendisplay.service.impl.electric;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.screen.screendisplay.entity.electric.ElectricOpermonEquipUse;
import com.screen.screendisplay.mapper.electric.ElectricOpermonEquipUseMapper;
import com.screen.screendisplay.service.electric.ElectricOpermonEquipUseService;
import com.screen.screendisplay.vo.electric.EquipmentUsageStatistics;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

/**
 * <p>
 * 运行检测设备状态监测表 服务实现类
 * </p>
 *
 * @author xuesong
 * @since 2025-08-23
 */
@Service
public class ElectricOpermonEquipUseServiceImpl extends ServiceImpl<ElectricOpermonEquipUseMapper, ElectricOpermonEquipUse> implements ElectricOpermonEquipUseService {

    @Override
    public EquipmentUsageStatistics getEquipmentUsageStatistics(Integer year) {
        // 查询指定年份的所有数据
        QueryWrapper<ElectricOpermonEquipUse> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("year", year);
        List<ElectricOpermonEquipUse> equipmentList = list(queryWrapper);
        
        // 初始化统计数据
        EquipmentUsageStatistics statistics = new EquipmentUsageStatistics();
        statistics.setYear(year);
        
        if (equipmentList.isEmpty()) {
            // 如果没有数据，返回0值
            statistics.setTotalUnits(0);
            statistics.setTotalInstalledCapacity(BigDecimal.ZERO);
            statistics.setRunningUnits(0);
            statistics.setRunningCapacity(BigDecimal.ZERO);
            statistics.setStandbyUnits(0);
            statistics.setStandbyCapacity(BigDecimal.ZERO);
            statistics.setMaintenanceUnits(0);
            statistics.setMaintenanceCapacity(BigDecimal.ZERO);
            return statistics;
        }
        
        // 计算统计数据（取最新一条记录的数据）
        ElectricOpermonEquipUse latestRecord = equipmentList.stream()
                .max((a, b) -> {
                    if (a.getMonth() != null && b.getMonth() != null && !a.getMonth().equals(b.getMonth())) {
                        return a.getMonth().compareTo(b.getMonth());
                    }
                    if (a.getDay() != null && b.getDay() != null) {
                        return a.getDay().compareTo(b.getDay());
                    }
                    return 0;
                })
                .orElse(equipmentList.get(0));
        
        statistics.setTotalUnits(latestRecord.getTotalUnits() != null ? latestRecord.getTotalUnits() : 0);
        statistics.setTotalInstalledCapacity(latestRecord.getTotalInstalledCapacity() != null ? latestRecord.getTotalInstalledCapacity() : BigDecimal.ZERO);
        statistics.setRunningUnits(latestRecord.getRunningUnits() != null ? latestRecord.getRunningUnits() : 0);
        statistics.setRunningCapacity(latestRecord.getRunningCapacity() != null ? latestRecord.getRunningCapacity() : BigDecimal.ZERO);
        statistics.setStandbyUnits(latestRecord.getStandbyUnits() != null ? latestRecord.getStandbyUnits() : 0);
        statistics.setStandbyCapacity(latestRecord.getStandbyCapacity() != null ? latestRecord.getStandbyCapacity() : BigDecimal.ZERO);
        statistics.setMaintenanceUnits(latestRecord.getMaintenanceUnits() != null ? latestRecord.getMaintenanceUnits() : 0);
        statistics.setMaintenanceCapacity(latestRecord.getMaintenanceCapacity() != null ? latestRecord.getMaintenanceCapacity() : BigDecimal.ZERO);
        
        return statistics;
    }

}
